<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button-group>
					<el-button type="primary" icon="el-icon-plus" @click="add">增加</el-button>
					<el-button type="danger" icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del" v-if="selection.length>0">删除</el-button>
				</el-button-group>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="主机地址" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="searchDb"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<jdTable ref="table" :apiObj="apiObj" :params="params" row-key="connId" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50" align="center"></el-table-column>
				<el-table-column label="" width="80" align="center">
					<template #default>
						<el-tag class="move" style="cursor: move;"><el-icon-d-caret style="width: 1em; height: 1em;"/></el-tag>
					</template>
				</el-table-column>
				<el-table-column label="类型" prop="connDrive" width="150"></el-table-column>
				<el-table-column label="别名" prop="connName" width="150"></el-table-column>
				<el-table-column label="主机地址" prop="connAddress" min-width="150"></el-table-column>
				<el-table-column label="数据库" prop="connLib" min-width="150"></el-table-column>
				<el-table-column label="端口" prop="connPort"></el-table-column>
				<el-table-column label="账号" prop="connUser" min-width="150"></el-table-column>
				<el-table-column label="状态" prop="connStatus" align="center" width="150">
					<template v-slot="scope">
						<el-switch v-model="scope.row.switchFlag" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" ></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="180"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="150">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary"  @click="edit(scope.row, scope.$index)" class="button">编辑</el-button>
							<el-popconfirm title="确定要删除吗？" @confirm="del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" class="button">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</jdTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	import Sortable from 'sortablejs'

	export default {
		name: 'role',
		components: {
			saveDialog,
			// permissionDialog
		},
		data() {
			return {
				ids: [],
				dialog: {
					save: false,
				},
				apiObj: this.$API.app.dbConn.listConnect,
				selection: [],
				params:{

				},
				search: {
					keyword: null
				},
				sw:true
			}
		},
		mounted() {
			this.rowDrop();
			// 自动展开菜单
			this.$store.commit('TOGGLE_IsCollapse', false);
		},
		methods: {
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async del(row){
				var res = await this.$API.app.dbConn.delConnect.delete(row.connId);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async () => {
					const loading = this.$loading();
					var res = await this.$API.app.dbConn.delConnect.delete(this.ids);
					if (res.code == 200){
						this.$refs.table.refresh()
						this.$message.success("操作成功")
					}
					loading.close();
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
				this.ids = selection.map(item => item.connId);
			},
			//表格内开关
			async changeSwitch(val, row) {
				row.connStatus = row.switchFlag == true ? 0 : 1
				row.$switch_status = true;
				var res = await this.$API.app.dbConn.changeStatus.put(row.connId);
				if (res.code == 200) {
					delete row.$switch_status;
					row.connStatus = val;
					this.$message.success("操作成功")
				} else {
					this.$message.error("操作失败")
				}
			},
			//搜索
			searchDb(){
				//connName
				this.params = {
					connAddress: this.search.keyword
				}
				this.$refs.table.reload(this.params);
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh()
				}else if(mode=='edit'){
					this.$refs.table.refresh()
				}
			},
			//行拖拽
			rowDrop(){
				const _this = this
				const tbody = this.$refs.table.$el.querySelector('.el-table__body-wrapper tbody')
				Sortable.create(tbody, {
					handle: ".move",
					animation: 300,
					ghostClass: "ghost",
					async onEnd({ newIndex, oldIndex }) {
						_this.loading = true;
						const tableData = _this.$refs.table.tableData
						const currRow = tableData.splice(oldIndex, 1)[0]
						tableData.splice(newIndex, 0, currRow)

						var res = {}
						// 数据排序
						res = await _this.$API.app.dbConn.changeSort.put(tableData);
						if(res.code == 200){
							_this.$refs.table.refresh();
							_this.$message.success("操作成功");
							_this.loading = false;
						}
					}
				})
			},
		}
	}
</script>

<style>
</style>
